package com.clive.llweb.service;

import com.clive.llweb.bean.Dishes;
import com.clive.llweb.exception.GlobalException;
import com.clive.llweb.mapper.DishMapper;
import com.clive.llweb.vo.DishesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

//Spring Ioc 注解 对象已经被new出来放大spring容器中  调用类里面的方法和属性
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;


    @Override
    public boolean addDishes(Dishes dishes) {
        Date date = new Date();
        dishes.setPicPath("aaa.jpg");
        dishes.setStatus(1);
        dishes.setBuyCount(0);
        dishes.setCreated(date);
        dishes.setUpdated(date);
        int i = dishMapper.addDishes(dishes);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public Dishes findDishesById(Integer id) {
        Dishes dishes = dishMapper.findDishById(id);
//        返回结果集
        return dishes;
    }

    @Override
    public boolean updateDish(Dishes dishes) {
        /**
         * 为什么有JSR303 还要手动判断菜品分类id是否为空
         假如在类上面用jsr303做判断  让菜品分类id一定不能为空
         但是做菜品分类添加的时候  菜品分类id一定为空  这样jsr303就会报空
         添加菜品分类的时候菜品分类id一定为空  里面就没有值  就不能完成修改操作
         */
        if(dishes.getCategory().getId()==null){
            throw new GlobalException("菜品分类id不能为空");
        }
        Date date = new Date();
        dishes.setUpdated(date);
        int i = dishMapper.updateDish(dishes);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public boolean deleteDish(Integer Id) {
        int i = dishMapper.deleteDish(Id);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public DishesVo queryDish(Dishes dishes) {
        DishesVo vo = new DishesVo();
        List<Dishes> list = dishMapper.queryDish(dishes);
        vo.setDishess(list);
        int dishesTotal = dishMapper.queryDishCount(dishes);
        vo.setDishestotal(dishesTotal);
        return vo;
    }
}
